#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os.path
import sys
import openpyxl
import xlrd


def read_xlsx(filename: str):
    workbook = openpyxl.load_workbook(filename)
    worksheet = workbook.worksheets[0]

    i = 0
    for row in worksheet.iter_rows(values_only=True):
        i += 1
        if i > 1:
            # 如果是真实业务程序代码, 这里常常会创建一个业务对象进行赋值
            print(row)


def read_xls(filename: str):
    wb = xlrd.open_workbook(filename=filename)
    table = wb.sheets()[0]
    row_count = table.nrows
    col_count = table.ncols

    for i in range(row_count):
        if i == 0:
            continue

        # 如果是真实业务程序代码, 这里常常会创建一个业务对象进行赋值
        for j in range(col_count):
            print(table.cell_value(i, j))


def read_excel(filename: str):
    if not os.path.isfile(filename):
        print('%s is not existed!' % filename)
        return

    if filename.endswith('.xlsx'):
        read_xlsx(filename)
    elif filename.endswith('.xls'):
        read_xls(filename)
    else:
        print('invalid file type of excel: ', filename)


def usage():
    print('usage: ./excel_reader.py [excel文件路径]')


def main():
    if len(sys.argv) < 2:
        usage()
    else:
        read_excel(sys.argv[1])


if __name__ == '__main__':
    main()
